﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TMS
{
    public partial class StocktakingControl : UserControl
    {
        Stocktaking stocktacking = new Stocktaking();
        bool edit = false;
        public StocktakingControl()
        {
            InitializeComponent();
            dgvStocktakingList.DataSource = stocktacking.GetAllStocktakingsFromDB().Tables[0];
            dgvStocktakingList.Columns[0].Visible = false;
            dgvStocktakingList.Columns[1].HeaderText = "Датум на попис";
            dgvStocktakingList.Columns[2].HeaderText = "Статус";

        }

        private void dgvStocktakingList_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            Program.GetMainForm.registerInHistoryMain(ControlsEnum.STOCKTAKING_CONTROL, null);
            Program.GetMainForm.ShowControl(ControlsEnum.STOCKTAKING_LISTS_CONTROL, dgvStocktakingList.CurrentRow);
            this.Hide();
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            dateTimePicker1.Visible = true;
            edit = false;
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (dgvStocktakingList.SelectedRows.Count > 0)
            {
                if (dgvStocktakingList.SelectedRows[0].Cells[2].Value.ToString() == "отворен")
                {
                    dateTimePicker1.Visible = true;
                    edit = true;
                }
                else
                {
                    MessageBox.Show("Не може да менувате затворен попис");
                }
            }
            else
                MessageBox.Show("Немате селектирано попис");
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            dateTimePicker1.Visible = false;
            stocktacking.Date = dateTimePicker1.Value;
            if (edit)
            {
                stocktacking.ID = Convert.ToInt32(dgvStocktakingList.SelectedRows[0].Cells[0].Value);
                stocktacking.UpdateStocktacking();
            }
            else
            stocktacking.AddStocktaking();
            Program.GetMainForm.ShowControl(ControlsEnum.STOCKTAKING_CONTROL);
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (dgvStocktakingList.SelectedRows.Count > 0)
            {
                if (dgvStocktakingList.SelectedRows[0].Cells[2].Value.ToString() == "отворен")
                {
                    stocktacking.ID = Convert.ToInt32(dgvStocktakingList.SelectedRows[0].Cells[0].Value);
                    if (stocktacking.GetAllStocktakingLists().Tables[0].Rows.Count == 0)
                    {
                        stocktacking.DeleteStocktacking();
                        this.Dispose();
                        Program.GetMainForm.ShowControl(ControlsEnum.STOCKTAKING_CONTROL);
                    }
                    else
                        MessageBox.Show("Не може да избришете попис кој содржи пописни листи");
                }
                else
                    MessageBox.Show("Не може да избришете затворен попис");


            }
            else
                MessageBox.Show("Немате селектирано попис");
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            if (dgvStocktakingList.SelectedRows.Count > 0)
            {
                if (dgvStocktakingList.SelectedRows[0].Cells[2].Value.ToString() == "отворен")
                {
                    Program.GetMainForm.ShowControl(ControlsEnum.STOCKTAKING_REPORT_CONTRO,dgvStocktakingList.SelectedRows[0]);
                    this.Dispose();
                }
                else
                    MessageBox.Show("Селектираниот попис е веќе затворен");
            }
            else
                MessageBox.Show("Немате селектирано попис");
        }

        private void btnReport_Click(object sender, EventArgs e)
        {
            if (dgvStocktakingList.SelectedRows.Count > 0)
            {
                Program.GetMainForm.registerInHistoryMain(ControlsEnum.STOCKTAKING_CONTROL, null);    
                Program.GetMainForm.ShowControl(ControlsEnum.STOCKTAKING_REPORT_CONTRO, dgvStocktakingList.SelectedRows[0]);
                this.Dispose();
            }
            else
                MessageBox.Show("Немате селектирано попис");
        }

        private void ToStBack_Click(object sender, EventArgs e)
        {
            if (Program.GetMainForm.Back())
                this.Dispose();
        }


        private void ToStForward_Click(object sender, EventArgs e)
        {
            if (Program.GetMainForm.Forward())
                this.Dispose();
        }

    }
}
